<?php
namespace app\restaurant\controller;

use think\Controller;
use think\Db;
use app\common\model\AgentManage;
use app\common\model\Area;
use think\Request;
use app\common\model\Types;
use app\common\model\Shops;

/**
* restaurant api
* @return 趙
*/
class Show extends Controller
{
	//获取代理id
	public function getAreaId($jing = '116.65716', $wei = '39.90966'){
        $key = 'SZSBZ-LFV33-YHK3A-YZZ77-YCVAQ-V4BYJ';
        $url = 'http://apis.map.qq.com/ws/geocoder/v1/?location=';
        $request = $url . $wei . ',' . $jing . '&key=' . $key;
        $res = json_decode(file_get_contents($request), true);
        $name = '%' . $res['result']['ad_info']['city'] . ',' . $res['result']['ad_info']['district'];
        $areaid = Area::where('merger_name', 'like', $name)->value('id');
        $agentid = AgentManage::where('agem_region', $areaid)->value('agem_id');
        return $agentid;
    }
   	//banner获取
    public function getrBanner(){
    	//ad_module 模块标识
        $time = time();
        $arr = [];
        $res=\app\common\model\Ad::where('ad_status',3)->where('ad_start_time<'.$time.' and ad_end_time>'.$time)->update(['ad_status'=>1]);

        $jing = $this->request->param('jing', '116.65716');
        $wei = $this->request->param('wei', '39.90966');
        if ((!empty($jing)) && (!empty($wei))) {
            $agentid = $this->getAreaId($jing, $wei);
            $url = Db::table('rbw_ad')->where('ad_agents_id', $agentid)->where('ad_status',1)->where('ad_start_time<'.$time.' and ad_end_time>'.$time)->where('ad_module','cy')->field('ad_label,ad_image_url,ad_content')->select();
            foreach ($url as $k => $v) {
                if ($url[$k]['ad_label'] == 1) {
                    $arr['banner'][] = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . '/' . $v['ad_image_url'];
                }
                if ($url[$k]['ad_label'] == 2) {
                    $arr['text_ads'][] = $v['ad_content'];
                }
                if ($url[$k]['ad_label'] == 3) {
                    $arr['foot_ads'][] = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . '/' . $v['ad_image_url'];
                }
            }
            if (!isset($arr['banner'])) {
                $banner = Db::table('rbw_ad')->where('ad_agents_id', null)->where('ad_status',1)->where('ad_label', 1)->field('ad_image_url,ad_content')->select();
                foreach ($banner as $k => $v) {
                    $arr['banner'][] = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . '/' . $v['ad_image_url'];
                }

            }

            if (!isset($arr['text_ads'])) {
                $banner = Db::table('rbw_ad')->where('ad_agents_id', null)->where('ad_status',1)->where('ad_label', 2)->field('ad_image_url,ad_content')->select();
                foreach ($banner as $k => $v) {
                    $arr['text_ads'][] = $v['ad_content'];
                }
            }
            if (!isset($arr['foot_ads'])) {
                $banner = Db::table('rbw_ad')->where('ad_agents_id', null)->where('ad_status',1)->where('ad_label', 3)->field('ad_image_url,ad_content')->select();
                foreach ($banner as $k => $v) {
                    $arr['foot_ads'][] = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . '/' . $v['ad_image_url'];
                }
            }
            return json($arr);
        } else {
            return json('错误');
        }
    }
	//分类
	function getrMent(){
		$map['ty_status'] = ['ty_status','=',1];
		$map['ty_module'] = ['ty_module','=','cy'];
        if ($this->request->param('ty_status') == 2) {
            $map['ty_status'] = ['ty_status','=',2];
        }
        //return $this->getOutput(200,'ok',$map);
        $types = Types::where($map)->limit(9)->select();
        foreach ($types as $k => $v) {
            $types[$k]['ty_ico'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$v['ty_ico'];
        }
        return json($types);
	}
	//推荐店铺
	function getrShop(){
		if(!empty($this->request->post('jing'))){
            //使用此函数计算得到结果后，带入sql查询。
            $jing = $this->request->post('jing');
            $wei = $this->request->post('wei');
            $squares = returnSquarePoint($jing, $wei, 5);//参数分别为经纬度 和距离单位公里
            $info_sql = "select sp_id,sp_name,sp_latitude,sp_longitude,sp_logo_url from `rbw_shops` where sp_module = 'cy' sp_audit = 1 and sp_latitude>{$squares['right-bottom']['lat']} and sp_latitude<{$squares['left-top']['lat']} and sp_longitude>{$squares['left-top']['lng']} and sp_longitude<{$squares['right-bottom']['lng']} order by ((sp_latitude-$wei)*(sp_latitude-$wei)+(sp_longitude-$jing)*(sp_longitude-$jing)) limit 6";
            /*在lat和lng上建立一个联合索引后，使用此项查询，每条记录的查询消耗平均为0.8毫秒，相比以前的1700ms，真的是天壤之别啊。效率真真的是以前的2125倍~~*/
            $res = Db::query($info_sql);
            foreach ($res as $k => $v) {
                $res[$k]['sp_logo_url'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$v['sp_logo_url'];
            }
            return json($res);
        }else{
            $shop = Db::table('rbw_shops')->where('sp_audit',1)->where('sp_module', 'cy')->field('sp_id,sp_name,sp_logo_url')->select();
            foreach ($shop as $k => $v) {
                $shop[$k]['sp_logo_url'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$v['sp_logo_url'];
            }
            return json($shop);
        }
	}
	//排序
	function getrSort(){
		$post = $this->request->post('sort');
		switch ($post['lable']) {
			case 1:
				//距离排序
				$jing = $this->request->post('jing');
        		$wei = $this->request->post('wei');
        		$shopAway = Db::query("select sp_id,sp_name,sp_logo_url from rbw_shops where sp_audit = 1 and sp_module = 'cy' order by ((sp_latitude-{$wei})*(sp_latitude-{$wei})+(sp_longitude-{$jing})*(sp_longitude-{$jing}))");
		      	foreach ($shopAway as $k => $v) {
		          	$shopAway[$k]['sp_logo_url'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$v['sp_logo_url'];
		      	}
      			return json($shopAway);
				break;
			case 2:
				//销量排序
				 $shopflow = Db::table('rbw_shops')->where('sp_status', 1)->where('sp_module', 'cy')->field('sp_id,sp_name,sp_logo_url')->order('sp_person_flow', 'desc')->limit(5)->select();
				 foreach ($shopflow as $k => $v) {
		          	$shopflow[$k]['sp_logo_url'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$v['sp_logo_url'];
		      	}
        		return json($shopflow);
        		break;
        	case 3:
        		//综合 shop表添加权重字段 根据权重占比 排序
        		$shopopen = Db::table('rbw_shops')->where('sp_status',1)->where('sp_module','cy')->fided('sp_id,sp_name,sp_logo_url')->limit(5)->select();
        		return json($shopopen);
			default:
				//默认
				echo "No number between 1 and 3";
				break;
		}
	}
}